/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jsf.beans;

import com.ocpsoft.pretty.faces.annotation.URLMapping;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.FacesException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.naming.InitialContext;

import project.dto.UsersDTO;
import project.facades.interfaces.UserManager;
import project.web.services.factories.UserActionFactory;

/**
 *
 * @author armdev
 */
@ManagedBean(name = "UserAdminBean")
@ViewScoped
//@URLMapping(id = "testBean", pattern = "/store/#{TestBean.category }/", viewId = "/next.jsf")
@URLMapping(id = "userlistBean", pattern = "/store/ouruserlist/", viewId = "/userlist.jsf")
public class UserAdminBean implements Serializable {

    private FacesContext context;
    private ExternalContext ex;
    private List<UsersDTO> userList = null;
    //@EJB(name ="project/facades/UserFacade/local")
    private UserManager userAction;

    /** Creates a new instance of UserAdminBean */
    public UserAdminBean() {
        context = FacesContext.getCurrentInstance();
        ex = context.getExternalContext();
        //init();
        userAction = UserActionFactory.getInstance();

    }

    public void init() {
    //    UserManager userAction = null;
        try {
            InitialContext context = new InitialContext();
            userAction = (UserManager) context.lookup("project/UserFacade/local");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<UsersDTO> getUserList() {
        return userAction.getUserList(0, 1000000);
    }

    private static void redirect(FacesContext facesContext, String url) {
        try {
            facesContext.getExternalContext().redirect(url);
        } catch (IOException e) {
            throw new FacesException("Cannot redirect to " + url + " due to IO exception.", e);
        }
    }
}
